package com.laiyima.zhongjiang.linghuilv.demo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.bean.TransactiondetailsBean;
import com.laiyima.zhongjiang.linghuilv.demo.home.TransactionInfoActivity;
import com.laiyima.zhongjiang.linghuilv.demo.home.TransactiondetailsActivity;

import java.text.DecimalFormat;
import java.util.List;

public class TransactiondetailsAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    NewTranAdapter adapter;
    private List<TransactiondetailsBean> mTransactiondetailsBean;
    private String TAG="AgentAdapter";

    public TransactiondetailsAdapter(TransactiondetailsActivity context, List<TransactiondetailsBean> list, NewTranAdapter adapter) {
        this.mInflater = LayoutInflater.from(context);
        this.mTransactiondetailsBean = list;
        this.adapter = adapter;
        this.mContext = context;

    }


    @Override
    public int getCount() {
        return mTransactiondetailsBean.size();
    }

    @Override
    public Object getItem(int position) {
        return mTransactiondetailsBean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.transactiondetails_item,null);
            viewHolder = new ViewHolder();
            viewHolder.trac_traId = convertView.findViewById(R.id.trac_traId);
            viewHolder.trac_name = convertView.findViewById(R.id.trac_name);
            viewHolder.trac_traDate = convertView.findViewById(R.id.trac_traDate);
            viewHolder.trac_traAmount = convertView.findViewById(R.id.trac_traAmount);
            viewHolder.trac_orderType = convertView.findViewById(R.id.trac_orderType);
            viewHolder.pd_imageView = convertView.findViewById(R.id.pd_imageView);
            convertView.setTag(viewHolder);

        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        TransactiondetailsBean transactiondetailsBean = mTransactiondetailsBean.get(position);
        viewHolder.trac_traId.setText(transactiondetailsBean.getTraId());
        viewHolder.trac_name.setText(transactiondetailsBean.getName());
        viewHolder.trac_orderType.setText(transactiondetailsBean.getOrderType());
        String orderType=transactiondetailsBean.getOrderType();
        if (orderType.equals("0")){
            viewHolder.pd_imageView.setImageResource(R.drawable.pd0);
        }else if(orderType.equals("1")){
            viewHolder.pd_imageView.setImageResource(R.drawable.td1);
        }
        else if(orderType.equals("2")){
            viewHolder.pd_imageView.setImageResource(R.drawable.td2);
        }
        else if(orderType.equals("3")){
            viewHolder.pd_imageView.setImageResource(R.drawable.td3);
        }
        else if(orderType.equals("4")){
            viewHolder.pd_imageView.setImageResource(R.drawable.td4);
        }
        else if(orderType.equals("5")){
            viewHolder.pd_imageView.setImageResource(R.drawable.td5);
        }
        else if(orderType.equals("6")){
            viewHolder.pd_imageView.setImageResource(R.drawable.td6);
        }
        else if(orderType.equals("7")){
            viewHolder.pd_imageView.setImageResource(R.drawable.td7);
        }
        else if(orderType.equals("8")){
            viewHolder.pd_imageView.setImageResource(R.drawable.td8);
        }
        else if(orderType.equals("9")){
            viewHolder.pd_imageView.setImageResource(R.drawable.td9);
        }
        else if(orderType.equals("10")){
            viewHolder.pd_imageView.setImageResource(R.drawable.td10);
        }

        String time=transactiondetailsBean.getTraDate();
         String t=time.substring(14,time.length());
        viewHolder.trac_traDate.setText(t);
        Double a1 = Double.parseDouble(transactiondetailsBean.getTraAmount());
        DecimalFormat decimalFormatq = new DecimalFormat("0.00");
        String b1 = decimalFormatq.format(a1);
        viewHolder.trac_traAmount.setText(b1+"元");

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ListView listView = (ListView) parent;
                TransactiondetailsBean mTransactiondetailsBean = (TransactiondetailsBean) listView.getItemAtPosition(position);
                String traid = mTransactiondetailsBean.getTraId();
                Intent Taxlistintent = new Intent(mContext, TransactionInfoActivity.class);
                Taxlistintent.putExtra("traid", traid);
                mContext.startActivity(Taxlistintent);
            }
        });


        return convertView;
    }

    public static class ViewHolder{
        ImageView pd_imageView;
        TextView trac_traId;
        TextView trac_name;
        TextView trac_traDate;
        TextView trac_traAmount;
        TextView trac_orderType;
    }



}
